package bg.code.commons.plugin.ui;

import bg.code.commons.builder.dto.ResultDTO;
import bg.code.commons.builder.idea.ProjectPluginConfig;
import bg.code.commons.builder.project.ToolConfigurationData;
import bg.code.commons.builder.utils.Strings;
import bg.code.commons.builder.vo.GenerateConfig;
import bg.code.commons.plugin.ui.table.AuthorJBPanel;
import bg.code.commons.plugin.ui.table.ClassJBPanel;
import bg.code.commons.plugin.ui.table.FieldJBPanel;
import bg.code.commons.plugin.ui.table.MethodParamJBPanel;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.panels.VerticalBox;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.table.TableModel;
import java.awt.*;
import java.text.MessageFormat;

import static bg.code.commons.plugin.Cons.*;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/19
 * 描述：
 */
public class PreviewData extends DialogWrapper {

    private ProjectPluginConfig pluginConfig;
    private GenerateConfig generateConfig;
    private ClassJBPanel classJBPanel;
    private FieldJBPanel fieldJBPanel;
    private AuthorJBPanel authorJBPanel;
    private MethodParamJBPanel methodParamJBPanel;

    private final int width;
    private final int height;

    public PreviewData(ProjectPluginConfig pluginConfig, GenerateConfig generateConfig) {
        super(pluginConfig.getProject());
        this.pluginConfig = pluginConfig;
        this.generateConfig = generateConfig;
        this.classJBPanel = new ClassJBPanel();
        this.fieldJBPanel = new FieldJBPanel(this.pluginConfig.getJavaFields());
        this.authorJBPanel = new AuthorJBPanel();
        this.methodParamJBPanel = new MethodParamJBPanel(generateConfig);

        this.initView();

        this.width = 1600;
        this.height = 800;
        this.setModal(true);
        this.setVerticalStretch(height);
        this.setHorizontalStretch(width);
        this.setResizable(true);
        this.setTitle(GENERATOR_TITLE_NAME);
        this.setOKButtonText(GENERATOR_OK_NAME);
        this.setCancelButtonText(GENERATOR_CANCEL_NAME);
        this.init();
    }

    private void initView() {
        this.classJBPanel.setClassFieldText(this.generateConfig.getBaseUri());
        this.authorJBPanel.getUserMailTextArea().setText(this.pluginConfig.getConfiguration().getUserMail());
        this.authorJBPanel.getAuthorTextArea().setText(this.pluginConfig.getConfiguration().getUserName());

        /** 文本改变事件 */
        this.classJBPanel.getObj().getDocument().addDocumentListener(new DocumentAdapter() {
            @Override
            protected void textChanged(@NotNull DocumentEvent documentEvent) {
                String baseUrlPrev = classJBPanel.getObj().getText();
                if (Strings.isNotBlank(baseUrlPrev)) {
                    methodParamJBPanel.setBaseUrl(baseUrlPrev);
                    TableModel model = methodParamJBPanel.getObj().getModel();
                    for (int i = model.getRowCount() - 1; i >= 0; i--) {
                        model.setValueAt(MessageFormat.format("{0}/{1}", baseUrlPrev, model.getValueAt(i, 4)), i, 5);
                    }
                }
            }
        });
    }

    @Override
    protected void doOKAction() {
        ToolConfigurationData configuration = pluginConfig.getConfiguration();
        if (Strings.isBlank(configuration.getTemplatePath())) {
            Messages.showErrorDialog("请先设置模板目录", "错误提示");
            return;
        } else {
            // 检查数据是否完整
            ResultDTO result = classJBPanel.errorMsg();
            if (!result.isOk()) {
                Messages.showErrorDialog(result.getMsg(), "错误提示");
                return;
            }

            result = authorJBPanel.errorMsg();
            if (!result.isOk()) {
                Messages.showErrorDialog(result.getMsg(), "错误提示");
                return;
            }

            result = methodParamJBPanel.errorMsg();
            if (!result.isOk()) {
                Messages.showErrorDialog(result.getMsg(), "错误提示");
                return;
            }


//            ErrorDialog.createError(pluginConfig.getProject() , "请先设置模板目录", "错误提示");
//            Path path = Paths.get(configuration.getTemplatePath());
//            File[] list = path.toFile().listFiles();
//            File[] var11 = list;
//            int var12 = list.length;
//
//            for(int var13 = 0; var13 < var12; ++var13) {
//                File file = var11[var13];
//                if (!file.isFile()) {
//                    this.templateSelect.addItem(file.getName());
//                }
//            }
        }
        super.doOKAction();
    }

    private Component getTopComponentLeftComponent() {
        return authorJBPanel.getComponent();
    }

    private Component getTopComponentRightComponent() {
        return methodParamJBPanel.getComponent();
    }

    private JSplitPane genJSplitPane() {
        // 创建分隔面板
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        // 设置左右两边显示的组件
        splitPane.setLeftComponent(getTopComponentLeftComponent());
        splitPane.setRightComponent(getTopComponentRightComponent());
        setJSplitPaneAttr(splitPane);
        // 设置分隔条的初始位置
        splitPane.setDividerLocation(300);
        return splitPane;
    }

    private void setJSplitPaneAttr(JSplitPane splitPane) {
        // 分隔条上显示快速 折叠/展开 两边组件的小按钮
        splitPane.setOneTouchExpandable(true);
        // 拖动分隔条时连续重绘组件
        splitPane.setContinuousLayout(true);
    }

    private Component getTopComponent() {
        JBPanel panel = new JBPanel();
        panel.setLayout(new BorderLayout());
        /*** 类请求路径 */
        panel.add(classJBPanel.getComponent(), BorderLayout.NORTH);
        /** 中间注释 与 方法区域*/
        panel.add(genJSplitPane(), BorderLayout.CENTER);
        return panel;
    }

    private Component getBottomComponent() {
        return fieldJBPanel.getComponent();
    }


    @Nullable
    @Override
    protected JComponent createCenterPanel() {

        JComponent component = super.createContentPane();
        VerticalBox root = new VerticalBox();
        /** 上下布局 */
        JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        setJSplitPaneAttr(splitPane2);
        splitPane2.setTopComponent(getTopComponent());
        splitPane2.setBottomComponent(getBottomComponent());

        root.add(splitPane2);
        root.add(component);
        return root;
    }
}
